<?php
namespace App\Services\Wxxcx;

use App\Services\BaseService as Service;
use Illuminate\Support\Facades\DB;

class BaseService extends Service
{
    /**
     * 服务类，写复杂的逻辑
     */

    //通过h5code，获取UID
    public function uidByH5code($h5code)
    {
        $res = (array) $this->dbConn($this->dbUser)
            ->where('h5code', $h5code)
            ->first();
        return empty($res) ? 0 : $res['id'];
    }

    /**
     * 获取门店
     */
    public function getShopByScode($scode)
    {
        return (array) $this->dbConn($this->dbShop)
            ->where('shop_code', $scode)
            ->first();
    }

    /**
     * 通过手机号，获取uid
     * @param string $phone
     * @return int
     */
    public function getUidByPhone($phone)
    {
        $userOne = (array) $this->dbConn($this->dbUser)
            ->where('phone', $phone)
            ->orderBy('id', 'desc')
            ->first();
        return $userOne ? $userOne['id'] : 0;
    }

    /**
     * 通过手机号，获取shopCodes
     * @param string $phone
     * @return int
     */
    public function getScodesByPhone($phone)
    {
        $shops = $this->dbConn($this->dbUs)
            ->where('phone', $phone)
            ->get();
        if (empty($shops)) return [];
        $arys = [];
        foreach ($shops as $v) {
            if (!is_array($v)) $v = (array) $v;
            $arys[] = $v['shop_code'];
        }
        return $arys;
    }

    /**
     * 获取码
     * @param string $code
     * @return int
     */
    public function getQrByCode($code)
    {
        $qr = (array) $this->dbConn($this->dbQr)
            ->where('code', $code)
            ->first();
        if (empty($qr)) {
            return array('code' => -2, 'msg' => '码不存在');
        }
        if ($qr['tmp_id'] > 0) {
            return array('code' => -2, 'msg' => '码已使用');
        }
        return array('code' => 0, 'msg' => '码可以使用');
    }

    //返回格式
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }
}